/*

Stickman Adventure - a game created by Daco & his cousin

Copyright (C) 2011-2012  DacoTaco & Wouterke

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


*/
#include "StaticObject.h"

//constructor for platforms with sound
Static_Object::Static_Object(const char* spritesheet,int xpos , int ypos, int h , int w,char Tile_image,const char* sound[],int soundlength) : Object(spritesheet,xpos,ypos,h,w,OBJECT_PLATFORM,sound,soundlength)
{
	initStaticObject(spritesheet ,Tile_image ,h ,w );

}
//constructor for platforms w/o sound
Static_Object::Static_Object(const char* spritesheet,int xpos , int ypos, int h , int w,char Tile_image) : Object(spritesheet,xpos,ypos,h,w,OBJECT_PLATFORM)
{
	initStaticObject(spritesheet ,Tile_image ,h ,w );
}

//constructor variable type with sound
Static_Object::Static_Object(const char* spritesheet,int xpos , int ypos, int h , int w,char type,char Tile_image,const char* sound[],int soundlength) : Object(spritesheet,xpos,ypos,h,w,type,sound,soundlength)
{
	initStaticObject(spritesheet ,Tile_image ,h ,w );
}
//constructor variable type w/o sound
Static_Object::Static_Object(const char* spritesheet,int xpos , int ypos, int h , int w,char type,char Tile_image) : Object(spritesheet,xpos,ypos,h,w,type)
{
	initStaticObject(spritesheet ,Tile_image ,h ,w );
}

//initializer is used 2 help construct the static object in the constructers
void Static_Object::initStaticObject(const char* spritesheet,char Tile_image, int h , int w){
	Static_Object::tile_image = Tile_image;
	if(spritesheet != NULL && spritesheet != "" && sheet != NULL)
	{
		orig_sprite = SPG_CopySurface(sheet);
		if(Tile_image == 0)
		{
			Static_Object::ResizeImage(sheet,h,w);
		}
	}
	else
		orig_sprite = NULL;
}

void Static_Object::ResizeImage(SDL_Surface *sprite,int h,int w)
{
	//zoom calc is what_it_should_be/what_it_is
	double zoomY = (double)h / (double)orig_sprite->h ;
	double zoomX = (double)w / (double)orig_sprite->w ;
	if(sprite)
		SDL_FreeSurface( sprite );
	sprite = rotozoomSurfaceXY(orig_sprite,0,zoomX,zoomY,0);
	return;
}
SDL_Rect* Static_Object::getImageClip()
{
	return NULL;
}
char Static_Object::Update(int collision,int y_ground, int* x_wall)
{
	if(tile_image != 0)
	{
		for(int z = 0;(z+Static_Object::sheet->w) < (x+width);)
		{
			//apply_surface(x+z,y,sheet,screen,0);
			z = z+ Static_Object::sheet->w;
		}
		return 0;
	}
	return 0;
}
void Static_Object::CollisionAction(Object* o, int collision)
{
	if(o == NULL ||o->getType() != OBJECT_PLAYER || type != OBJECT_ENEMY)
		return;
	if(this->getType() == OBJECT_ENEMY)
	{
		((MovingObject*)o)->death();
	}
	//o->CollisionAction(this);

}
void Static_Object::draw(SDL_Surface* destination,Object* screen)
{
	if(destination != NULL )
	{
		if(sheet != NULL)
		{
			if(tile_image != 0)
			{
				for(int z = 0;z < width;z=z+sheet->w)
				{
					//blit only what is in the actuall screen. fuck the rest :P
					if(	x+z+sheet->w >= screen->GetColRect()->x && //the right of the object is in screen AND
						x+z <= ( screen->GetColRect()->x + screen->getWidth() ) ) //the left of the object is in screen
					{
						for(int y2 = 0;y2 < height;y2=y2+sheet->h)
						{
#ifdef OGL_MODE
							//void apply_surface( int x, int y, int h,int w, GLuint *texture, SDL_Rect* clip)
							apply_surface(x+z-screen->GetColRect()->x,y+y2-screen->GetColRect()->y,sheet->h,sheet->w,&texture,getImageClip());
#else
							//apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip)
							apply_surface(x+z-screen->GetColRect()->x,y+y2-screen->GetColRect()->y,sheet,destination,getImageClip());
#endif
						}
					}
				}
			}
			else
			{
#ifdef OGL_MODE
				//TODO : for some odd reason this is tiling the image with the clip? o_O;
				//hacky fix -> apply_surface(x-screen->getXPosition(),y-screen->getYPosition(),this->getImage()->h,this->getImage()->w,&texture,NULL);
				//NOTE : ask some1 to tell me wtf is going on with apply_surface(x,y,this->getImage()->h,this->getImage()->w,&texture,getImageClip(screen));
				apply_surface(x-screen->getXPosition(),y-screen->getYPosition(),height,width,&texture,getImageClip());
				//doesn't work (tiles :/ ) : apply_surface(screen->getXPosition(),screen->getYPosition(),height,width,&texture,screen->getImageClip());
#else
				//SDL_Rect* tet = screen->getImageClip();
				apply_surface(x,y,sheet,destination,screen->getImageClip());
				//apply_surface(x-screen->getXPosition(),y-screen->getYPosition(),sheet,destination,NULL);
#endif
			}
		}
		else
		{
			//TODO : adjust this to only draw on screen
			int colour = 0x0e9747;
			if(type != OBJECT_BACKGROUND)
				colour = 0xFFFFFF;
			SDL_Rect rect = *Static_Object::GetColRect();
			rect.x = x - screen->GetColRect()->x;
			rect.y = y - screen->GetColRect()->y;
			apply_surface(&rect,destination,colour);
		}
	}
}